using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Shrapnel
{
	public partial class View
	{

		/// <summary>
		/// Buffer that is used for copy/cut/pasting.
		/// </summary>
		private string copiedElementsBuffer;

		/// <summary>
		/// Do we have anything in our copiedElementsBuffer?
		/// </summary>
		private bool hasCopiedElements = false;

		/// <summary>
		/// The current pasting offset.
		/// </summary>
		private PointF currentPasteOffset = new PointF(0, 0);

		/// <summary>
		/// The offset we give all elements from their original upon pasting
		/// </summary>
		private SizeF pasteOffset = new SizeF(80, 80);


		/// <summary>
		/// The offset we give all elements from their original upon pasting
		/// </summary>
		public System.Drawing.SizeF PasteOffset
		{
			get { return pasteOffset; }
			set { pasteOffset = value; }
		}


		/// <summary>
		/// Reset our copy/cut buffer
		/// </summary>
		public void ResetCopyBuffer()
		{
			hasCopiedElements = false;
			currentPasteOffset = new PointF(0, 0);
		}


		/// <summary>
		/// Copy the selection to memory
		/// </summary>
		public void CopySelection()
		{
			hasCopiedElements = true;
			currentPasteOffset = new PointF(0, 0);
			copiedElementsBuffer = SerializeSelectionToString();
		}


		/// <summary>
		/// Cut the selection to memory and remove from our workspace
		/// </summary>
		public void CutSelection()
		{
			hasCopiedElements = true;
			currentPasteOffset = new PointF(0, 0);
			copiedElementsBuffer = SerializeSelectionToString();
			DeleteSelection();
		}


		/// <summary>
		/// Paste any previously copied elements
		/// </summary>
		public void PasteSelection()
		{
			if (hasCopiedElements)
			{
				currentPasteOffset += pasteOffset;
				Selection selection = DeserializeSelectionFromString(copiedElementsBuffer);

				SizeF offset = new SizeF(currentPasteOffset);

				// Add an offset to all pasted elements
				foreach (Element element in selection)
				{
					element.Position += offset;
				}

				// Update all areas for our cables
				foreach (Element element in selection)
				{
					if (element is Cable)
					{
						((Cable)element).RecalculateOccupiedArea();
					}
				}

				Invalidate();
			}
		}

	}
}
